package www.qiyue.chengtou2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.qiyue.utils.UUIDGenerator;

import java.util.*;

@RestController
public class CT2ACT {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String test(){
        String sql="SELECT b.BUSINESS_KEY_,c.ID_,c.CREATE_TIME_  FROM act_hi_procinst b,act_ru_task c WHERE c.PROC_INST_ID_=b.ID_ AND CONVERT (b.BUSINESS_KEY_ USING gbk) IN (SELECT CONVERT (c.id USING gbk) AS id FROM act_ru_task a,act_hi_procinst b,reis_contract c WHERE CONVERT (b.BUSINESS_KEY_ USING gbk)=CONVERT (c.id USING gbk) AND a.PROC_INST_ID_=b.ID_ GROUP BY c.id HAVING count(c.id)> 1) ORDER BY b.BUSINESS_KEY_,c.CREATE_TIME_";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> arrayList = new ArrayList<>();
        for (int i = 0; i < mapList.size(); i++) {
            String business_key_ = mapList.get(i).get("BUSINESS_KEY_").toString();
            String id_ = mapList.get(i).get("ID_").toString();
            Date date = (Date) mapList.get(i).get("CREATE_TIME_");
            for (int j = i+1; j < mapList.size(); j++) {
                if(mapList.get(j).get("BUSINESS_KEY_").toString().equals(business_key_)){
                    String business_key_2 = mapList.get(j).get("BUSINESS_KEY_").toString();
                    String id_2 = mapList.get(j).get("ID_").toString();
                    Date date2 = (Date) mapList.get(j).get("CREATE_TIME_");
                    if (date.getTime()<date2.getTime()){
                        String update="UPDATE act_ru_task set ASSIGNEE_='1067246875800000001' WHERE ID_='"+id_+"'";
                        jdbcTemplate.update(update);
                    }else {
                        String update="UPDATE act_ru_task set ASSIGNEE_='1067246875800000001' WHERE ID_='"+id_2+"'";
                        jdbcTemplate.update(update);
                    }
                }
            }
        }
        return null;
    }
}
